package com.leileimann.tour.controller;

import com.leileimann.tour.controller.AdminPages.AdSSpotController;
import com.leileimann.tour.controller.Userpages.CityController;
import com.leileimann.tour.entity.*;
import com.leileimann.tour.mapper.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author leileiMann
 * @date 2022/3/2 15:46
 * 用于页面跳转，，默认访问主页面为index.html
 */
@Controller
@RequestMapping("index")
public class IndexController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SSpotMapper sSpotMapper;
    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private RestaurantMapper restaurantMapper;
    @Autowired
    private ShopPlaceMapper shopPlaceMapper;
    @Autowired
    private CityController cityController;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private AdSSpotController adSSpotController;

//    用户
    /**
     * 转入用户登录页面
     * */
    @RequestMapping("/login")
    public String login(){
        return "userpage/login";
    }

    /**
     * 登陆成功后转向带有user对象的主页
     * */
    @RequestMapping("/view")
    public String view(Model model){
        List<SSpot> guiyangsspot = cityController.getTop10SSpot();
        model.addAttribute("guiyangsspot",guiyangsspot);
        return "userpage/view";
    }

    /**
     * 转向用户注册页面
     * */
    @RequestMapping("/register")
    public String register(){
        return "userpage/register";
    }

//    /**
//     * 当登录成功时候返回主页，主页上方的登录就变成用户名
//     * 运行说明：post方式发送请求 ,先将获取到的页面中session中包含的手机号的用户，将其赋给User对象
//     * */
//    @RequestMapping(value = "/info",produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
//    @ResponseBody
//    public User info(HttpSession session){
//        User u = (User)session.getAttribute("user");
//        return userMapper.findByTel(u.getTel());
//    }

    /**
     * 用户注销操作
     * */
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        /*将user的session变成空就行*/
        session.setAttribute("user",null);
        return "/index";
    }

    /**转向个人中心*/
    @RequestMapping("/person")
    public String person(HttpSession session,Model model){
        User user = (User)session.getAttribute("user");
        model.addAttribute("user",user);
        return "userpage/person";
    }

    /**转向收藏*/
    @RequestMapping("/sc")
    public String sc(HttpSession session, Model model){
        User user = (User) session.getAttribute("user");
        List<Collect> collectList = collectMapper.getAllCollectByTel(user.getTel());
        List<SSpot> sspotlist = new ArrayList<>();
        //遍历每一个收藏，再通过收藏的景点名称检索景区详情
        for (Collect collectList1:collectList){
            SSpot sSpot = sSpotMapper.getSSpotByCollectname(collectList1.getCollectname());
            sspotlist.add(sSpot);
        }
        model.addAttribute("collectSSpot",sspotlist);
        return "userpage/sc";
    }
//城市
    /**
     * 转向城市页面，显示城市
    * */
    @RequestMapping("/city")
    public String city(Model model,@RequestParam(defaultValue = "1",value = "pageNum")int pageNum){
        List<City> cities = cityController.getAllCity(pageNum);
        /*获取城市数量*/
        int count = cityMapper.getNumOfCity();
        model.addAttribute("cities",cities);
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("count",count);
        return "userpage/cities";
    }

    /**
     * 跳转到相应城市的详情页,包含景点、酒店和购物点
     * */
    @RequestMapping("/toCityDeatil")
    public String toCityDeatil(Model model, String cityname,@RequestParam(defaultValue = "1",value = "pageNum1")int pageNum1,
                               @RequestParam(defaultValue = "1",value = "pageNum2")int pageNum2,
                               @RequestParam(defaultValue = "1",value = "pageNum3")int pageNum3){
        double count1 = Math.ceil(sSpotMapper.getSSpotNumByCityName(cityname)/12);
        double count2 = Math.ceil(hotelMapper.getHotelNumByCityName(cityname)/12);
        double count3 = Math.ceil(sSpotMapper.getSSpotNumByCityName(cityname)/12);
        City city = cityController.getCityByCityName(cityname);
        List<SSpot> sSpots = sSpotMapper.getSSpotByCityName(cityname,pageNum1);
        List<Hotel> hotels = hotelMapper.getHotelByCityname(cityname,pageNum2);
        List<ShopPlace> shopPlaces = cityController.getShopPlaceByCityName(cityname,pageNum3);
        /*将搜索到的相应的数据放在model中，前端通过读取model中的数据，来显示后端传入的数据*/
        model.addAttribute("sspots",sSpots);
        model.addAttribute("hotels",hotels);
        model.addAttribute("shopPlaces",shopPlaces);
        model.addAttribute("city",city);
        model.addAttribute("pageNum1",pageNum1);
        model.addAttribute("pageNum2",pageNum2);
        model.addAttribute("pageNum3",pageNum3);
        model.addAttribute("count1",count1);
        model.addAttribute("count2",count2);
        model.addAttribute("count3",count3);
        return "userpage/CityDeatil";
    }

/*景区相关页面*/
    /**
     * 转向景区页面
     * */
    @RequestMapping("/sspot")
    public String sspot(Model model,@RequestParam(defaultValue = "1",value = "pageNum")int pageNum){
        /*通过设置页面显示范围查询，mapper中也定义了相应的CQL语句*/
        /*Math.ceil(XXX)将XXX向上取整，这里获取了景区的数量*/
        double count = Math.ceil(sSpotMapper.getSSpotNum()/10);
        List<SSpot> sSpots = cityController.getAllSSpot(pageNum);
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("sSpots",sSpots);
        model.addAttribute("count",count);
        return "userpage/SSpot";
    }

    /**
     * 跳转到相应景区详情页面，包含餐厅 显示景点详情
     * */
    @RequestMapping("/toSSpotDeatil")
    public String toSSpotDeatil(HttpSession session,Model model,String sspotname){
        SSpot sSpot = sSpotMapper.getSSpotDeatil(sspotname);
        List<Restaurant> restaurants =  restaurantMapper.getRestaurantBySSpotName(sspotname);
        User user = (User) session.getAttribute("user");
        Object obj = collectMapper.getObjByCollectName(sspotname, user.getTel());
        if(obj != null){
            Collect collect = (Collect) obj;
            model.addAttribute("collect",collect);
        }
        model.addAttribute("sspot",sSpot);
        model.addAttribute("restaurants",restaurants);
        return "userpage/SSpotDeatil";
    }

    /**
     * 查询成功转向相应城市详情页面
     * */
    @RequestMapping("/searchCity")
    public String searchCity(Model model,@Param("modules")String modules,@RequestParam(defaultValue = "1",value = "pageNum")int pageNum){
        double count = Math.ceil(sSpotMapper.getSSpotNumByCityName(modules)/12);
        City city = cityMapper.getCityByCityName(modules);
        List<SSpot> sspots = sSpotMapper.getSSpotByCityName(modules,pageNum);
        List<ShopPlace> shopPlaces = shopPlaceMapper.getShopByCityName(modules,pageNum);
        List<Hotel> hotels = hotelMapper.getHotelByCityname(modules,pageNum);
        model.addAttribute("shopPlaces",shopPlaces);
        model.addAttribute("hotels",hotels);
        model.addAttribute("sspots",sspots);
        model.addAttribute("city",city);
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("count",count);
        return "userpage/searchCity";
    }

//餐厅相关页面
    /**
     * 进入餐厅详情
     * */
    @RequestMapping("/toResDeatil")
    public String toResDeatil(Model model,String restaurantname){
        Restaurant restaurant = restaurantMapper.getRestaurantByName(restaurantname);
        model.addAttribute("res",restaurant);
        return "userpage/ResDeatil";
    }

//其他页面
    /**
     * 跳转到其他页面,将相应的酒店、餐厅、购物点的实体数据放置进model中
     * */
    @RequestMapping("/toOther")
    public String other(Model model,@RequestParam(defaultValue = "1",value = "pageNum")int pageNum){
        List<Hotel> hotelList = hotelMapper.getAllHot();
        List<Restaurant> restaurants = restaurantMapper.getAllRestaurant();
        List<ShopPlace> shopPlaceList = shopPlaceMapper.getAllShopPlaces();
        model.addAttribute("hotelList",hotelList);
        model.addAttribute("restaurants",restaurants);
        model.addAttribute("shopPlaceList",shopPlaceList);
        return "userpage/Other";
    }


//管理员页面跳转用
    /**进入管理员登录页*/
    @RequestMapping("/admin")
    public String adminLogin(){
        return "admin/adminLogin";
    }

    /**转向景区管理页面*/
    @RequestMapping("/SSpotManger")
    public String SSpotManger(Model model,@RequestParam(defaultValue = "1",value = "pageNum")int pageNum){
        List<SSpot> adsSpotList = adSSpotController.showSSpotByPageNum(pageNum);
        double count = Math.ceil(sSpotMapper.getSSpotNum()/10);
        model.addAttribute("adsSpotList",adsSpotList);
        model.addAttribute("adPageNum",pageNum);
        model.addAttribute("count",count);
        return "admin/SSpotManger";
    }


}
